package com.kingsailor.gunsmith.shiro;

import com.kingsailor.gunsmith.security.BaseAppUser;
import com.kingsailor.gunsmith.security.LoginUserAccessor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import java.io.Serializable;

public class LoginUserAccessorImpl implements LoginUserAccessor {
    @Override
    public Serializable getSessionId() {
        return getSession().getId();
    }

    @Override
    public String getCsrfToken() {
        return getBaseAppUser().getCsrfToken();
    }

    @Override
    public long getSessionTimeout() {
        return getSession().getTimeout();
    }

    static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    static BaseAppUser getBaseAppUser() {
        return (BaseAppUser) SecurityUtils.getSubject().getPrincipal();
    }
}
